package com.chenjt.netty.five_thrift;

import com.chenjt.netty.five_thrift.generated.PersonService;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.layered.TFramedTransport;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2024/8/12 14:51
 */
public class ThriftServer {

    public static void main(String[] args) throws Exception {
        TNonblockingServerSocket socket = new TNonblockingServerSocket(8899);

        THsHaServer.Args arg = new THsHaServer.Args(socket).minWorkerThreads(2).maxWorkerThreads(4);
        PersonService.Processor<PersonServiceImpl> processor = new PersonService.Processor<>(new PersonServiceImpl());

        // 协议层（TCompactProtocol：二进制压缩协议，会把生成的字节码尽最大的努力压缩成容量最小的字节码）
        arg.protocolFactory(new TCompactProtocol.Factory());
        // 传输层
        arg.transportFactory(new TFramedTransport.Factory());
        arg.processorFactory(new TProcessorFactory(processor));


        THsHaServer server = new THsHaServer(arg);

        System.out.println("Thrift Server Started!");

        // 异步非阻塞（死循环），一直监听客户端发送过来的消息
        server.serve();


    }

}
